from playerlib import info
check = info["version"].endswith
if check("a") or check("b") or check("c") or check("d") or check("e") or check("f"):
    import playerlib9g as playerlib
else:
    import playerlib
# note until ES is updated GG will import it's own copy of  playerlib oy9g :)
# this is for stability mainly (and tidiness)
# When playerlib is updated

import ggExceptions

'''
An extension to playerlib.Player, adding lots of nice GG functionality

 - A lot of the code in here is borrowed from Playerlib (thx Don!)
'''


class ggPlayerException(ggExceptions._ggException):
    '''
    Throw a player exception 
    '''
    pass
    
    
class _ggPlayer(playerlib.Player):
    '''
    A major extension to the playerlib.Player class adding serious GG like functions specially for the user!
    '''
    pass
    
class _ggPlayerController(object):
    '''
    A specialist object for managing the players on the server
    '''
    players = {}
    filterlist = {}
    
    def getUseridList(self):
        '''
        Return all the userids currently being tracked
        '''
        temp = []
        for id in self.players:
            temp.append(int(id))
        return temp
        
    def getPlayerList(self, filter=None):
        '''
        Retrieves a list of Player objects.
        '''
        templist = self.players
        if filter is not None:
            filter = filter.strip(' ')
            # use the filtername to lookup from filters dictionary
            # apply this filter to the playerlist
            # I have no idea if this works, I've never tried filter() before
            # playerlist = filter(filters[filtername], playerlist)
            for y in filter.split(","):
                templist = filter(filters[y], templist)
        return templist
        
pcontrol = _ggPlayerController()

    
# ===========
# Public methods
# ===========

def getPlayer(userid):
    '''
    Grabs the player instance from the controller
    '''
    id = int(userid)
    if id in pcontrol.players:
        return pcontrol.players[id]
    return False
   
def createPlayer(userid):
    '''
    Adds a player to the controller
    '''
    id = int(userid)
    if id not in pcontrol.players:
        pcontrol.players[id] = _ggPlayer(id)
        return True
    return False
    
def destroyPlayer(userid):
    '''
    Removes a player from the controller
    '''
    id = int(userid)
    if id in pcontrol.players:
        pcontrol.players.pop(id,0)
        return True
    return False
    
def getUseridList(filter="#all"):
    '''
    Grabs a list of all the userid's currently being tracked (according to an optional filter)
    '''
    if not filter: return # return ALL the users
    if filter == "#all":
        return pcontrol.getUseridList()
    elif filter[0] == "#":
        return map(int, pcontrol.getPlayerList(filter))
    else:
        raise ggPlayerException "Invalid filter provided to ggPlayerlib.getUseridList: " + filter
        
def getPlayerList(filter=None):
    '''
    Get a list of the player instances according to a filter
    '''
    return pcontrol.getPlayerList(filter)